package lab03.test12;

import java.sql.*;

public class Test12 {

    public static void main(String[] args) {


        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            connection = DriverManager.getConnection("jdbc:mysql:" +
                            "//localhost:3306/java_2017?serverTimezone=UTC",
                    "root",
                    "123"
            );

            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM t_users ");
            System.out.println("查询所有记录：");
            while (resultSet.next()) {
                System.out.print(resultSet.getString(1) + " ");
                System.out.print(resultSet.getString(2) + " ");
                System.out.print(resultSet.getString(3) + " ");
                System.out.print(resultSet.getString(4));
            }
            System.out.println();
            resultSet = statement.executeQuery("select  * from t_users where usename='admin'");
            System.out.println("查询admin记录:");
            while (resultSet.next()) {
                System.out.print(resultSet.getString(1) + " ");
                System.out.print(resultSet.getString(2) + " ");
                System.out.print(resultSet.getString(3) + " ");
                System.out.print(resultSet.getString(4));

            }
            System.out.println();
            resultSet = statement.executeQuery("select * from t_users where password='123456'" +
                    " and usename='admin'");
            System.out.println("查询用户名为'admin'，密码为'123456'的记录");
            while (resultSet.next()) {
                System.out.print(resultSet.getString(1) + " ");
                System.out.print(resultSet.getString(2) + " ");
                System.out.print(resultSet.getString(3) + " ");
                System.out.print(resultSet.getString(4));
            }
            System.out.println();
            resultSet = statement.executeQuery("select * from t_users" +
                    " where register_datetime>10000000");
            System.out.println("查询注册时间大于10000000的记录");
            while (resultSet.next()) {
                System.out.print(resultSet.getString(1) + " ");
                System.out.print(resultSet.getString(2) + " ");
                System.out.print(resultSet.getString(3) + " ");
                System.out.print(resultSet.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
